package cn.happy.favdish.view.adapter

import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import cn.happy.favdish.R
import cn.happy.favdish.databinding.ItemDishLayoutBinding
import cn.happy.favdish.model.entities.FavDish
import cn.happy.favdish.utils.Constants
import cn.happy.favdish.view.activities.AddUpdateDishActivity
import cn.happy.favdish.view.fragments.FavoriteDishesFragment
import cn.happy.favdish.view.fragments.HomeFragment
import com.bumptech.glide.Glide

/**
 * Author: created by ghappy on 2024/8/13 10:57
 *
 * Description:
 */
class FavDishAdapter (private val fragment:Fragment): RecyclerView.Adapter<FavDishAdapter.ViewHolder>(){

    private var dishes: List<FavDish> = listOf()

    class ViewHolder(view:ItemDishLayoutBinding) : RecyclerView.ViewHolder(view.root){
        val ivDishImage = view.ivDishImage
        val tvTitle = view.tvDishTitle
        val ibMore = view.ibMore
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding: ItemDishLayoutBinding = ItemDishLayoutBinding.inflate(
            LayoutInflater.from(fragment.context),parent,false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return dishes.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val dish = dishes[position]
        Glide.with(fragment).load(dish.image).into(holder.ivDishImage)
        holder.tvTitle.text = dish.title
        holder.itemView.setOnClickListener{
            if(fragment is HomeFragment){
                fragment.dishDetails(dish)
            }

            if(fragment is FavoriteDishesFragment){
                fragment.dishDetails(dish)
            }
        }

        holder.ibMore.setOnClickListener{
            val popup = PopupMenu(fragment.context,holder.ibMore)
            popup.menuInflater.inflate(R.menu.menu_adapter,popup.menu)
            popup.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.action_edit_dish -> {
                        val intent = Intent(fragment.requireActivity(), AddUpdateDishActivity::class.java)
                        intent.putExtra(Constants.EXTRA_DISH_DETAILS,dish)
                        fragment.requireActivity().startActivity(intent)
                        true
                    }
                    R.id.action_remove_dish -> {
                        if(fragment is HomeFragment){
                           fragment.deleteDish(favDish = dish)
                        }
                        true
                    }
                    else -> true
                }
            }
            popup.show()
        }

        if(fragment is HomeFragment){
            holder.ibMore.visibility = View.VISIBLE
        }

        if(fragment is FavoriteDishesFragment){
            holder.ibMore.visibility = View.GONE
        }
    }

    fun dishesList(list:List<FavDish>){
        dishes = list
        notifyDataSetChanged()
    }
}